package net.bwie.realtime.jtp.dwd.log.job;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.jtp.dwd.log.function.AdjustIsNewProcessFunction;
import net.bwie.realtime.jtp.dwd.log.function.AppLogSplitProcessFunction;
import net.bwie.realtime.jtp.utils.KafkaUtil;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * DWD层数据应用开发：将ODS层采集原始日志数据，进行分类处理，存储Kafka队列。
 *      数据流向：kafka -> flink datastream -> kafka
 * @author xuanyu
 * @date 2025/5/18
 */

public class JtpAppLogEtIJob {
    public static void main(String[] args) throws Exception {
        // 1.执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 2.数据源-source
        DataStream<String> kafkaDataStream = KafkaUtil.consumerKafka(env, "topic-log");
//        kafkaDataStream.print("kafka");
        // 3.数据转换-transformation
        DataStream<String> pageStream = processLog(kafkaDataStream);
        pageStream.print("page");

        // 4.数据输出-sink
        KafkaUtil.producerKafka(pageStream,"dwd-traffic-page-log");
        // 5.触发执行-execute
        env.execute("JtpLogEtlJob") ;
    }
    /**
     * 对实时获取APP流量日志数据进行ETL处理，并且进行分流
     *      1-数据清洗
     *      2-新老访客状态标记修复
     *      3-数据分流
     *      4-存储数据(非页面日志存储)
     * @param stream app日志流
     */
    private static DataStream<String> processLog(DataStream<String> stream) {
        //1-数据清洗
        DataStream<String> jsonStream = appLogCleaned(stream);
        //2-新老访客状态标记修复
//        DataStream<String> etIStream = processIsNew(jsonStream);
        //3-数据分流
        DataStream<String> pageStream = spliStream(jsonStream);

        //返回数据流
        return pageStream;
    }

    /**
     * 3-日志数据分流
     * @param stream
     * @return
     */

    private static DataStream<String> spliStream(DataStream<String> stream) {
        // 第1步、数据侧输出流标签
        final OutputTag<String> errorTag = new OutputTag<String>("error-log"){};
        final OutputTag<String> startTag = new OutputTag<String>("start-log"){};
        final OutputTag<String> displayTag = new OutputTag<String>("display-log"){};
        final OutputTag<String> actionTag = new OutputTag<String>("action-log"){};


        // 第2步、日志分流处理
        SingleOutputStreamOperator<String> pageStream = stream.process(
                new AppLogSplitProcessFunction(errorTag, startTag, displayTag, actionTag)
        );
        // 第3步、测流输出
        DataStream<String> errorStream = pageStream.getSideOutput(errorTag);
        KafkaUtil.producerKafka(errorStream,"dwd-traffic-error-log");
        DataStream<String> startStream = pageStream.getSideOutput(startTag);
        KafkaUtil.producerKafka(startStream,"dwd-traffic-start-log");
        DataStream<String> displayStream = pageStream.getSideOutput(displayTag);
        KafkaUtil.producerKafka(displayStream,"dwd-traffic-display-log");
        DataStream<String> actionStream = pageStream.getSideOutput(actionTag);
        KafkaUtil.producerKafka(actionStream,"dwd-traffic-action-log");


        // 第4步、输出主流
        return pageStream;
    }

    /**
     * 2-新老访客状态标记修复
     * @param stream
     * @return
     */
    private static DataStream<String> processIsNew(DataStream<String> stream) {
        KeyedStream<String, String> midStream = stream.keyBy(
                new KeySelector<String, String>() {
                    @Override
                    public String getKey(String value) throws Exception {
                        //value值数据格式
                        /*
                        {
                          "common": {
                              "ar": "230000",
                              "ba": "iPhone",
                              "ch": "Appstor",
                              "is_new": "1",
                              "md": "iPhone Xs",
                              "mid": "mid_740591",
                              "os": "iOS 13.3.1",
                              "uid": "793",
                              "vc": "v2.1.134"
                          },
                          "page": {
                              "during_time": 14243,
                              "page_id": "home"
                          },
                          "ts": 1736321812000
                         }
                         */
                        //经过上述分析可知 需要进过2次
                        return JSON.parseObject(value).getJSONObject("common").getString("mid");
                    }
                }
        );
        //b-状态编程 对is_new校验修复
        DataStream<String> isNewStream=midStream.process(new AdjustIsNewProcessFunction());

        //c-返回数据流
        return isNewStream;
    }
    /**
     * 对APP原始日志数据进行清洗，去除无效数据
     * @param stream app原始日志数据
     * @return
     **/

    private static DataStream<String> appLogCleaned(DataStream<String> stream) {

        //a-脏数据侧边输出时标记
        final OutputTag<String> dirtyTag = new OutputTag<String>("dirty-log") {};

        //b-数据清洗处理
        SingleOutputStreamOperator<String> cleanedStream =stream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String value,Context ctx, Collector<String> out) throws Exception {
              try {
                  //a.解析json数据
                  JSON.parseObject(value);
                  //b.没有异常，解析正确，正常输出
                  out.collect(value);
              }catch (Exception e){
                  //c.捕获异常，侧边流输出数据
                  ctx.output(dirtyTag,value);
              }
            }
        });
        //c-侧边输出数据:脏数据
        SideOutputDataStream<String> dirtyStream = cleanedStream.getSideOutput(dirtyTag);
        KafkaUtil.producerKafka(dirtyStream,"dwd-traffic-dirty-log");

        return cleanedStream;
    }

}
